<template>
	<!-- 店员列表 -->
	<view class="clerkList-main">
		<!-- 搜索 -->
		<view class="searchPage-input">
			<uni-search-bar
				radius="50"
				:value="searchValue"
				style="flex: 1"
				placeholder="请输入搜索内容"
				clearButton="always"
				cancelButton="none"
				@confirm="getSearch"
				@cancel="getSearch"
			/>
			<text class="searchPage-title" @click="getSearch">搜索</text>
		</view>

		<view class="clerkList" v-if="staffList.length > 0">
			<view class="clerkList-item" v-for="(item, index) in staffList" :key="item.id">
				<view class="clerk-change">
					<u-switch v-model="item.status" @change="(e) => getChange(e, item.id)" size="20" inactiveValue="0" activeValue="1"></u-switch>
				</view>
				<view class="clerkImage">
					<image :src="imageUrls(item.user.avatar)" mode="widthFix" class="images"></image>
				</view>
				<view class="clerkInfo">
					<view class="name">{{ item.user.nickname || '暂无名称' }}</view>
					<view class="iphone">{{ item.user.mobile || '暂无手机号' }}</view>
				</view>
			</view>
		</view>

		<view v-else>
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
		</view>
	</view>
</template>

<script>
import { imageUrl } from '../../../utils/tools.js';
import { storeStaffList, storeStaffDel, storeStaffForbid, storeStaffEnable } from '../../../api/flashManage/index.js';
export default {
	data() {
		return {
			staffList: [],
			storeType: '',
			searchValue: ''
		};
	},
	onLoad() {
		this.storeType = this.$store.state.storeType;
		this.getstoreStaffList();
	},
	methods: {
		// 店员列表
		async getstoreStaffList() {
			try {
				const queryData = {
					type: this.storeType
				};
				if (this.searchValue) {
					queryData.keyword = this.searchValue;
				}
				const resData = await storeStaffList(queryData);
				let staffListData = resData.data;
				this.staffList = staffListData.filter((item) => {
					return item.status == 0 || item.status == 1;
				});
			} catch (e) {
				throw Error(e);
			}
		},

		// 店员禁用
		async getstoreStaffForbid(id) {
			const resData = await storeStaffForbid({
				type: this.storeType,
				id
			});
			uni.showToast({
				title: '禁用成功',
				icon: 'none'
			});
		},

		// 店员启用
		async getstoreStaffEnable(id) {
			const resData = await storeStaffEnable({
				type: this.storeType,
				id
			});
			uni.showToast({
				title: '启用成功',
				icon: 'none'
			});
		},

		// 启用or禁用
		getChange(index, id) {
			if (index == 0) {
				this.getstoreStaffForbid(id);
			} else {
				this.getstoreStaffEnable(id);
			}
		},

		// 搜索
		getSearch() {
			this.getstoreStaffList();
		},

		imageUrls(url) {
			return imageUrl(url);
		}
	}
};
</script>

<style scoped lang="scss">
.clerkList-main {
	.searchPage-input {
		display: flex;
		align-items: center;
		height: 120rpx;
		background-color: #fff;
		.searchPage-title {
			margin-right: 20rpx;
			color: #444;
			font-size: 26rpx;
		}
	}

	.clerkList {
		box-sizing: border-box;
		padding: 30rpx;
		.clerkList-item {
			background-color: #fff;
			box-sizing: border-box;
			padding: 20rpx 15rpx;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;
			.clerk-change {
				margin-left: 10rpx;
				margin-right: 25rpx;
			}
			.clerkImage {
				width: 140rpx;
				.images {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
				}
			}

			.clerkInfo {
				flex: 1;
				font-size: 26rpx;
				.name {
					font-weight: bold;
					font-size: 28rpx;
				}
				.iphone {
					margin: 10rpx 0;
				}
			}

			.clerkIcon {
				width: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				font-size: 26rpx;
				color: #444;
			}
		}
	}
}
</style>
